package com.dmx.net;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class HostInfoExtractor {
	private String userName = "";

	private String password = "";

	private String host = "";

	private int port = 0;

	private String protocol = "";

	private URL theUrl;

	private static final Log log = LogFactory.getLog(HostInfoExtractor.class);

	private static final Pattern pRipFile = Pattern.compile("([^/]+)$");

	public HostInfoExtractor(URL url) {

		theUrl = url;

		setHost(url.getHost());
		if (url.getPort() == -1) {
			setPort(url.getDefaultPort());
		} else {
			setPort(url.getPort());
		}

		String userInfo = url.getUserInfo();
		String userName = "";
		String passwd = "";
		if (userInfo != null) {
			if (userInfo.contains(":") == false) {
				userName = userInfo;
			} else {

				try {
					String[] splitArr = userInfo.split(":");
					userName = splitArr[0];
					passwd = splitArr[1];
				} catch (ArrayIndexOutOfBoundsException e) {

				}
			}
		}

		setUserName(userName);
		setPassword(passwd);
	}

	public String extractFileName() {
		String ret = "/";

		String path = theUrl.getPath();

		Matcher m = pRipFile.matcher(path);

		if (m.find()) {
			ret = m.group(1);
		}

		return ret;
	}

	public String extractTheFolderFullUrl() throws Exception {

		String fullUrl = theUrl.getProtocol() + "://" + this.getUserName()
				+ ":" + this.getPassword() + "@" + this.getHost() + ":"
				+ this.getPort() + this.extractTheFolder();

		return fullUrl;
	}

	public String extractTheFolder() throws MalformedURLException {
		String ret = "/";
		String fileName = null;

		String path = theUrl.getPath();

		Matcher m = pRipFile.matcher(path);

		if (m.find()) {
			fileName = m.group(1);
		}

		if (null == fileName) {
			return path;
		}

		try {
			ret = path.substring(0, path.lastIndexOf(fileName));
		} catch (Exception e) {
			String erMsg = "failed to extract the folder of url:"
					+ theUrl.toExternalForm();
			log.error(erMsg);
			throw new MalformedURLException(erMsg);
		}

		return ret;
	}

	public void decorateNetworkProtocolClient(
			NetworkProtocolClient networkProtocolClient)
			throws NotSupportedProtocolException {

		if (networkProtocolClient.getProtocol().equalsIgnoreCase(
				this.getProtocol())) {
			throw new NotSupportedProtocolException(
					"networkProtocolClient is not for the protocol:" + protocol);
		}

		networkProtocolClient.setUserName(this.getUserName());
		networkProtocolClient.setPasswd(this.getPassword());
		networkProtocolClient.setPort(this.getPort());
		networkProtocolClient.setRemoteHost(this.getHost());
	}

	public String getHost() {
		return host;
	}

	protected void setHost(String host) {
		this.host = host;
	}

	public String getPassword() {
		return password;
	}

	protected void setPassword(String password) {
		this.password = password;
	}

	public int getPort() {
		if (-1 == port) {
			return theUrl.getDefaultPort();
		}
		return port;
	}

	protected void setPort(int port) {
		this.port = port;
	}

	public String getProtocol() {
		return protocol;
	}

	protected void setProtocol(String protocol) {
		this.protocol = protocol;
	}

	public String getUserName() {
		return userName;
	}

	protected void setUserName(String userName) {
		this.userName = userName;
	}
}
